package com.sharkburner.aeris.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.sharkburner.aeris.dao.ArticleDao;
import com.sharkburner.aeris.dao.entity.Article;
import com.sharkburner.aeris.dao.entity.ArticleState;
import com.sharkburner.aeris.dao.entity.User;
import com.sharkburner.aeris.dao.entity.UserSite;
import com.sharkburner.aeris.dao.entity.UserSiteArticle;
import com.sharkburner.common.dao.impl.GenericDao;

/**
 * Article DAO, all operations over articles
 * 
 * @author inartea
 * 
 */
@Repository
public class ArticleDaoImpl extends GenericDao<Article> implements ArticleDao {

    @Override
    public synchronized void save(Article article) {
	super.save(article);
    }

    @SuppressWarnings("unchecked")
    @Override
    public DetachedCriteria findAll() {
	DetachedCriteria criteria = DetachedCriteria.forClass(Article.class);
	// getSession().createCriteria(Article.class);

	return criteria;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Article> findAll(User user) {
	StringBuilder hql = new StringBuilder();
	hql.append(" SELECT usa.article ");
	hql.append(" FROM ").append(UserSiteArticle.class.getName());
	hql.append(" usa join usa.userSite userSite ");
	if (user != null && user.getId() > 0) {
	    hql.append(" where userSite.user=:user");
	}

	Query query = getSession().createQuery(hql.toString());
	query.setResultTransformer(Transformers.aliasToBean(Article.class));
	if (user != null && user.getId() > 0) {
	    query.setParameter("user", user);
	}

	return query.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Article> findAll(UserSite userSite) {
	StringBuilder hql = new StringBuilder();
	hql.append(" SELECT usa.article ");
	hql.append(" FROM ").append(UserSiteArticle.class.getName())
		.append(" usa ");
	hql.append(" where usa.userSite=:userSite");

	Query query = getSession().createQuery(hql.toString());
	query.setResultTransformer(Transformers.aliasToBean(Article.class));

	query.setParameter("userSite", userSite);

	return query.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Article> findAll(UserSite userSite, ArticleState state) {
	StringBuilder hql = new StringBuilder();
	hql.append(" SELECT usa.article ");
	hql.append(" FROM ").append(UserSiteArticle.class.getName())
		.append(" usa ");
	hql.append(" where usa.userSite=:userSite and usa.articleState=:state");

	Query query = getSession().createQuery(hql.toString());
	query.setResultTransformer(Transformers.aliasToBean(Article.class));

	query.setParameter("userSite", userSite);
	query.setParameter("state", state);

	return query.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Article> findByLink(String link) {
	StringBuilder hql = new StringBuilder();
	hql.append(" FROM ").append(Article.class.getName());
	hql.append(" where link=:link");

	Query query = getSession().createQuery(hql.toString());
	query.setResultTransformer(Transformers.aliasToBean(Article.class));
	query.setParameter("link", link);

	return query.list();
    }

}
